import Opt from '@/components/dt/info/Opt.js'
import ruleOpt from '@/biz/option/rule.js'
import toolMgr from '@/biz/base/tool.js'
import comMgr from '@/biz/sales/salesCommon'
import dt from '@dt/dt'
import channelMgr from '@/biz/channel/channel.js'

let _this = null
let channelList = []


class NewOpt extends Opt {

  constructor(info, vm) {
    _this = vm
    super(info)
    this.build()
  }

  build() {
    this.createAll()
    this.opts.push(...this.part1)
  }


  createAll() {
    this.newType = {
      title: '修改后渠道合作类型',
      col: 20,
      key: 'newCooperationTypeName',
      visible: false,
      text: {}
    }

    // let filesEcho = []
    // if (_this.info.address && _this.info.address > 0) {
    //   _this.info.address.forEach(item => {
    //     if (item) {
    //       filesEcho.push({
    //         name: item.accessoryName,
    //         url: item.accessoryUrl
    //       })
    //     }
    //   })
    // }


    this.file = {
      title: '渠道附件',
      col: 20,
      key: 'address',
      visible: false,
      uploadImg: {
        api: toolMgr.unloadFile(),

        multiple: true,
        show: false,
        maxlength: 30,
        format: ['jpg', 'jpeg', 'png'],
        formatErr: () => {
          this.loading = false
          this.$Message.error('只支持上传jpg、jpeg、png图片！')
        },
        preview: (file) => {
          console.log(file, 'file222222');
          toolMgr.downloadFile(file.url, file.name)
        },
      },
      data: {
        to: (v) => {
          console.log(_this.info.address, this.file, v, 'vvvvvvvvv');
          let filesEcho = []
          if (_this.info.address && _this.info.address.length > 0) {
            _this.info.address.forEach(item => {
              if (item) {
                console.log('1222222222');
                filesEcho.push({
                  name: item.date,
                  url: item.url
                })
              }
            })
          }

          let address1 = v.map(item => {
            return {
              date: '',
              url: item.response ? item.response.data.imageURL : item.url
            }
          })


          for (var i = 0; i < address1.length; i++) {
            let _index = filesEcho.findIndex((val) => {
              return val.url === address1[i]
                .url
            });
            if (_index != '-1') {
              filesEcho.splice(_index, 1)
            }

          }
          let address = filesEcho.concat(address1)
          console.log(address, filesEcho, 'adddddddd');
          return {
            address
          }
        }
      },
    }
    this.newChannel = {
      title: '修改后客户来源渠道',
      key: 'channelId',
      col: 24,
      load: (o) => {
        let params = {
          validate: 1,
          type: 1
        }
        channelMgr.caseChannelListOrder(params).then(res => {
          res.forEach(item => {
            item.key = item.channelId
            item.value = item.channelName
          })
          let annexFiles = []
          let currentChannel = res.find(item => {
            return item.channelName == _this.info.channelSourceName
          })
          if (currentChannel && (currentChannel.isNetAcquire == '1' || currentChannel
              .isTakeLook ==
              '1')) {
            if (currentChannel.cooperationType == '7') {
              this.file.visible = false
            } else {
              this.file.visible = true
              if (_this.info.annexLogList && _this.info.annexLogList.length) {
                let currentAnnex = _this.info.annexLogList.find(annexItem => {
                  return annexItem.channelId == currentChannel.channelId
                })
                if (currentAnnex && currentAnnex.url) {
                  let annexUrl = JSON.parse(currentAnnex.url)
                  if (annexUrl && annexUrl
                    .length > 0) {
                    annexUrl.forEach(itemUrl => {
                      if (itemUrl) {
                        annexFiles.push({
                          name: itemUrl.date,
                          url: itemUrl.url
                        })
                      }
                    })
                    _this.$set(_this.info, 'address', annexFiles)
                  }
                }
              }
            }


          } else {
            this.file.visible = false
          }
          o.radioNine.options = res
          channelList = res
        })
      },
      radioNine: {
        options: [],
      },
      change: (res) => {
        let annexFiles = []
        let currentChannel = channelList.find(item => {
          return item.channelId == res.value
        })

        _this.info.newChannelSourceName = currentChannel.channelName
        _this.info.newCooperationTypeName = currentChannel.cooperationTypeName

        if (currentChannel && (currentChannel.isNetAcquire == '1' || currentChannel
            .isTakeLook == '1')) {
          if (currentChannel.cooperationType == '7') {
            this.file.visible = false
          } else {
            this.file.visible = true
            if (_this.info.annexLogList && _this.info.annexLogList.length) {
              let currentAnnex = _this.info.annexLogList.find(annexItem => {
                return annexItem.channelId == currentChannel.channelId
              })
              console.log(currentAnnex, 'currentAnnex');
              if (currentAnnex && currentAnnex.url) {
                let annexUrl = JSON.parse(currentAnnex.url)
                if (annexUrl && annexUrl
                  .length > 0) {
                  annexUrl.forEach(itemUrl => {
                    if (itemUrl) {
                      annexFiles.push({
                        name: itemUrl.date,
                        url: itemUrl.url
                      })
                    }
                  })
                  _this.$set(_this.info, 'address', annexFiles)
                }
              } else {
                _this.$set(_this.info, 'address', [])
              }
            }
          }


        } else {
          this.file.visible = false
        }
        this.newType.visible = res.value ? true : false
      },

      rules: [
        ruleOpt.required('请选择修改后客户来源渠道', "number")
      ],
      // data: {
      //   from: info => info.newChannel,
      //   to: (res) => {
      //     this.file.visible = res.length > 1 && (res[0].label == "渠道带看" || res[0].label == "网络媒体")
      //     this.newType.visible = res.length > 1
      //     return {
      //       channelId: (res.length > 1 ? res[1].value : null),
      //       newChannelSourceName: (res.length > 1 ? res[1].label : null),
      //       newCooperationTypeName: (res.length > 1 ? res[1].cooperationTypeName ?? '无' : '无'),
      //       newChannel: res.length > 1 ? res : null
      //     }
      //   }
      // }
    }
    // clueCustomerMgr.channelSource().then((res) => {
    //   // this.newChannel.cascader.options = res.channelList
    //   // res.channelList.some(itm => {
    //   //   return itm.children.some(i => {
    //   //     if (i.label == this.info.channelSourceName) {
    //   //       this.info.cooperationTypeName = i.cooperationTypeName
    //   //     }
    //   //     return i.label == this.info.channelSourceName
    //   //   })
    //   // })
    // })

    // channelList.forEach(item => {
    // })
    // this.file.visible = true

    this.part1 = [{
        title: '当前客户来源渠道',
        key: 'channelSourceName',
        col: 20,
        text: {}
      },
      {
        title: '当前渠道合作类型',
        key: 'cooperationTypeName',
        col: 20,
        text: {}
      },
      this.newChannel,
      this.newType,
      this.file
    ]
  }
}

export default NewOpt